//
//  HJCircleProgress.m
//  XLCircleProgressDemo
//
//  Created by xiatiejun on 2022/11/11.
//  Copyright © 2022 Apple. All rights reserved.
//

#import "HJCircleProgress.h"

@interface HJCircleProgress()

@property (nonatomic, assign) CGFloat lineWidth;
@property (nonatomic, strong) CAShapeLayer *progressLayer;

@end


@implementation HJCircleProgress


-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _lineWidth = 8;
        
        CGFloat centerX = self.bounds.size.width * 0.5;
        CGFloat centerY = self.bounds.size.height * 0.5;
        CGFloat radius = (self.bounds.size.width - _lineWidth) * 0.5;
        
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius startAngle:(-0.5f*M_PI) endAngle:1.5f * M_PI clockwise:YES];
        
        CAShapeLayer *backgroundLayer = [CAShapeLayer layer];
        backgroundLayer.frame = self.bounds;
        backgroundLayer.fillColor = [[UIColor clearColor] CGColor];
        backgroundLayer.strokeColor = UIColorMake(0xE8E0FF).CGColor;
        backgroundLayer.lineWidth = _lineWidth;
        backgroundLayer.path = [path CGPath];
        backgroundLayer.strokeEnd = 1;
        [self.layer addSublayer:backgroundLayer];
        
        CAShapeLayer *progressLayer = [CAShapeLayer layer];
        progressLayer.frame = self.bounds;
        progressLayer.fillColor =  [[UIColor clearColor] CGColor];
        progressLayer.strokeColor  = [[UIColor tj_mainColor] CGColor];
        progressLayer.lineCap = kCALineCapRound;
        progressLayer.lineWidth = _lineWidth;
        progressLayer.path = [path CGPath];
        progressLayer.strokeEnd = 0;
        [self.layer addSublayer:progressLayer];

        _progressLayer = progressLayer;
    }
    return self;
}
 
- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    _progressLayer.strokeEnd = progress;
}

@end
